package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.content.common.domain.page.PageRequestParams;
import com.xuecheng.content.common.domain.page.PageVO;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    /*
    测试查询所有
     */
    @GetMapping("/all")
    public List<CourseBase> queryAll(){
        List<CourseBase> courseBases = courseBaseService.queryAll();
        return courseBases;
    }


    /*
        根据课程名称和课程的审核状态分页查询
         */
    @Override
    @PostMapping("/course/list")
    public PageVO queryCourseBaseList(PageRequestParams params,@RequestBody QueryCourseModel model) {

        //获取令牌---待完善  todo
        //通过工具类获得公司和用户信息
        Long companyId = UAASecurityUtil.getCompanyId();

        PageVO pageVO = courseBaseService.queryCourseList(params, model,companyId);

        return pageVO;
    }
    /*
    新增课程基本信息
     */

    @PostMapping("/course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {

        //通过工具类将前端传过来的vo转为dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);

        //通过工具类获取用户信息以及公司
        Long companyId = UAASecurityUtil.getCompanyId();

        //设置公司和用户信息
        courseBaseDTO.setCompanyId(companyId);
        //调用服务层

        return courseBaseService.createCourseBase(courseBaseDTO);
    }

    /*
    修改-根据id查询课程基本信息
     */
    @GetMapping(value = "/course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        //获取公司的id值
        Long companyId = UAASecurityUtil.getCompanyId();
        //获取课程的基本信息
        CourseBaseDTO courseBase = courseBaseService.getCourseBase(courseBaseId, companyId);
        return courseBase;
    }

    /*
    修改课程基本信息
     */
    @Override
    @PutMapping("/course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {

        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        //通过工具类获得公司以及用户信息
        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseDTO.setCompanyId(companyId);


        CourseBaseDTO courseBaseDTO1 = courseBaseService.modifyCourseBase(courseBaseDTO);

        return courseBaseDTO1;
    }
    /*
    根据id删除课程基本信息
     */
    @Override
    @DeleteMapping("/course/{courseBaseId}")
    public void removeCourseBase(@PathVariable long courseBaseId) {

//        获得前端的用户信息和课程
        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseBaseId,companyId);
    }
    /*
    提交审核
     */
    @Override
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCouseBase(@PathVariable Long courseBaseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.commitCourseBase(courseBaseId,companyId);
    }



}
